<?php
/**
 * Created by PhpStorm.
 * User: johnwang
 * Date: 2020/4/24
 * Time: 上午10:19
 */

namespace app\api\service;


use think\Log;

/**
 * Class
 * @package app\api\service
 *
 * 美葩传媒
 */
class VbillService2
{

    protected $privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDi8nU8DprJPE5t
Esuff+7sFuW+d2CbCdtAMLyP7r9VoaraVtusOwUpTcgpOcnwnJ8zZOZhm3j2U+ZX
JUk4LmsX/9i7ZcPAs604H23NbD5EePl8xLr6vZhPzHnIV+LEk3ZqilG+l/pigd4O
iFiYMtD3T+DUA+DiJRA9EK0ZzWaUBWMmbxh2zhgp5DD7O3f/ExfNpGhFQCpj91nt
okSnB2bqnJu2srR815itw7vZ+60pDwVnj0jSGOqPhWuESpfjBqVx8jxN5XmlihVi
7Zz9wSjoYtE1o3u8UlPA2PkEi/9uqS9Z+kAL54yzEbAl1AVVvbakXnO8vLVdpuXF
KBTgA8bZAgMBAAECggEAO8JOXn8WARFRMWV2msxMNHyfCHVUyd5hjliG/nuGhFzq
qU2f0I1GAadKophl9Wa4gQb7pw4SXwKer6nQyutmJc6n2kiksBRf81xLRIFRMCSO
xWzkM2xDNBgKpVeHDe4qi0la1w4bz16VA0yhz4np/oVZFVrkF4hc60d93UJ51VXK
4lMBn2JLws4kHWw4epLUEsAXXjKvwPmjID5B91I0agYWcmiOWaC4J4a9SkLKDWMc
+tdlo4OjHphyyf5wvDbcbzcTkiddwFcDPYd7utnmIQ2JsTO23WFcuEE8EzCqY7/W
vfRyK8G6UzfKr6l/LtK5vFAZ+nUHDDKDZo/LsPj5RQKBgQDxXqbiEBZs89PD+w2r
kIxTQlqnsBLChZyjhvcoHhv3MbZmXFBqCUv7Kpr+Z5uzmoU/TYfjsyXV9bSM6ELq
c2VhTaMTQcvoHPpiDNz85Rp/CCd9Ppa8lyGPW6LXD7BMnAy289XBGZCyBFH0Dwyu
aIwzqPuYUgTU5709u83wOs9bGwKBgQ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xAaMoExWQM2ipf/1qGdsWmkZScPflBqg7m0olOD87ymAVP/3Tcbvi34bDfwIDAQAB";

    protected $url = "https://openapi.tianquetech.com";
    protected $testUrl = "https://openapi-test.tianquetech.com";

    protected $jsapiScan = "/order/jsapiScan";
    protected $refund = "/order/refund";
    protected $commitApply = "/merchant/realName/commitApply";
    protected $orgId = "59244336";

    protected $uploadUrl = "/merchant/uploadPicture";//上传图片
    protected $merchantUrl = "/merchant/income";//进件
    protected $queryMerchantInfo = "/merchant/queryMerchantInfo";
    protected $editMerchantInfo = "/merchant/editMerchantInfo";//编辑
    protected $updateMerchantInfo = "/merchant/updateMerchantInfo";//修改商户信息
    protected $weixinVerify = "/merchant/realName/commitApply";//微信认证
    protected $queryApplyInfo = "/merchant/realName/queryApplyInfo";//微信实名认证查询
    protected $queryBalance = "/capital/query/queryBalance";//查询余额
    protected $reverseScan = "/order/reverseScan";
    protected $querySettlement = "/capital/query/querySettlement";//结算查询
    protected $poundageQuery = "/query/poundageQuery";//手续费查询
    protected $queryGrantStatus = "/merchant/realName/queryGrantStatus";//微信授权查询

    protected $merchantInfoQuery = "/merchant/merchantInfoQuery";//商户信息查询
    protected $getFileUrl = "/capital/fileDownload/getFileUrl";//对账文件


    public function __construct()
    {

        import('.sxf.AopClient', EXTEND_PATH, '.php');

    }

    /**
     * 聚合支付
     */
    public function jsApi($req)
    {


//        $req = array();
//        $req['mno'] = $param['mno'];
//        $req['ordNo'] = $param['ordNo'];
//        $req['amt'] = $param['amt'];
//        $req['payType'] = $param['payType'];
//        $req['payWay'] = $param['payWay'];
//        $req['subject'] = $param['subject'];
//        $req['tradeSource'] = $param['tradeSource'];
//        $req['trmIp'] = $param['trmIp'];
//        $req['subAppid'] = $param['subAppid'];
//        $req['notifyUrl'] = $param['notifyUrl'];

        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => getRandom(32),
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );

        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);

        $reqStr = json_encode($reqStr, 320);

        $url = $this->url . $this->jsapiScan;
        $resp = $aopClient->curl($url, $reqStr);

        return $resp;
    }


    /**
     * 主扫
     */
    public function scanPay($req = array())
    {

        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => getRandom(32),
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );

        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);
        $url = $this->url . $this->reverseScan;
        $resp = $aopClient->curl($url, $reqStr);

        return $resp;

    }


    /**
     * @param array $param
     * @throws \Exception
     * 退款
     */
    public function refund($req)
    {

        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => getRandom(32),
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );

        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);
        $url = $this->url . $this->refund;
        $resp = $aopClient->curl($url, $reqStr);

        return $resp;
    }

    /**
     * 微信实名认证
     */
    public function weixinVerify($req)
    {


        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => getRandom(32),
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );

        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);
        $url = $this->url . $this->commitApply;
        $resp = $aopClient->curl($url, $reqStr);

        return $resp;

    }

    /**
     * 入网
     */
    public function merchant($req, $isUpdate)
    {

        $reqId = getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);
        Log::info($reqStr);

        if ($isUpdate == 1) {
            $requestUrl = $this->url . $this->merchantUrl;
        } else {
            $requestUrl = $this->url . $this->updateMerchantInfo;

        }
        $resp = $aopClient->curl($requestUrl, $reqStr);

        return $resp;
    }

    /**
     * 上传图片
     */
    public function upload($req)
    {

        /**
         * 3. 上传随行付图片服务器
         */
        $post_data = array(
            "file" => new \CURLFile($req['filePath']),
            'orgId' => $this->orgId,  //文件类型
            'pictureType' => $req['type'],
            'reqId' => getRandom(32),
        );
        $ch = curl_init();
        //  设置变量
        curl_setopt($ch, CURLOPT_URL, $this->url . $this->uploadUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//执行结果是否被返回，0是返回，1是不返回
        curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置，是否显示头部信息，1为显示，0为不显示
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 100);//设置curl执行超时时间最大是多少
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //   执行并获取结果
        $sxfRet = curl_exec($ch);//运行curl
        curl_close($ch);

        return $sxfRet;

    }

    /**
     * 入驻结果查询
     */
    public function queryMerchant($req)
    {


        $reqId = getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->queryMerchantInfo;

        $resp = $aopClient->curl($requestUrl, $reqStr);

        return $resp;
    }

    /**
     * 微信实名认证查询
     */

    public function queryWeixinVerify($req)
    {


        $reqId = getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->queryApplyInfo;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }

    public function settle($req)
    {
        $reqId = getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );
        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->querySettlement;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }


    /**
     *
     * 查询余额
     *
     */

    public function balance($req)
    {


        $reqId = getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->queryBalance;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }

    /**
     * 手续费查询
     */
    public function feeQuery($req)
    {


        $reqId = getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->poundageQuery;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }

    public function queryStatus($req)
    {

        $reqId = getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->queryGrantStatus;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }

    /**
     * 对账文件下载
     */
    public function getFileUrl($req)
    {
        $reqId = getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );
        error_log("====getFile==" . json_encode($reqJson));


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->getFileUrl;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;

    }


    /**
     * 商户信息查询
     */
    public function merchatQuery($req)
    {

        $reqId = getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->merchantInfoQuery;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }

}